#encoding=utf8
'''
Created on 2012-9-24

@author: Administrator
'''
from pub import Func
from pub.Config import Config
class Room(object):
    '''
    房间对象模型
    '''
    TABLE_NAME = "sq_room"
    PRIMARY_KEY = "Id"
    ROOMID = "Id"
    ROOMNAME = "RoomName"
    INTRODUCE = "Introduce"
    ROOMTYPE = "RoomType"
    UPLIMIT = "UpLimit"
    CREATOR = "Creator"
    CREATED = "Created"
    ROOMFIRSTNAME = "RoomFirstName"
    ROOMPRI = "RoomPri"
    COMMEND = "Commend"
    ROOMISSHOW = "RoomIsShow"
    def __init__(self):
        '''
        Constructor
        '''
#      房间编号  
        self.__Id = Config.I_SYS_ZERO
#      房间名
        self.__RoomName = Config.S_SYS_NONE
#      房间人数上限  
        self.__UpperLimit = Config.I_ROOM_UP_LIMIT
#      创建时间  
        self.__Created = Func.getNow()
#      房间首字  
        self.__FirstName = Config.S_SYS_NONE
#      房间简介  
        self.__Introduce = Config.S_SYS_NONE
#      推荐排序字段  
        self.__Commend = Config.I_SYS_ONE
#      房间类型  
        self.__RoomType = Config.I_ROOM_TYPE_SYSTEM
#      创建人  
        self.__Creator = Config.I_SYS_ZERO
#      房间用户列表  
        self.__userList = []
        self.__userNameList={}
#        房间是否展示
        self.__isShow = Config.I_SYS_ONE
#        房间是否私有
        self.__roomPri = Config.I_SYS_ZERO
        self.__roomOwner=None;
        
        self.__state=Config.I_ROOM_STATE_READY;
        
        self.__usedIdiom ={};
        #一轮中已使用过的成语
        
        #当前成语首字
        self.__currentWord="";

    def get_current_word(self):
        return self.__currentWord


    def set_current_word(self, value):
        self.__currentWord = value

    
    
    #重置房间    
    def reset(self):
        self.set_state(Config.I_ROOM_STATE_OVER);
        self.set_room_owner(None);
        self.__usedIdiom={};
        self.____userList=[];
        self.__userNameList={}
        

        

    def get_used_idiom(self):
        return self.__usedIdiom


    def set_used_idiom(self, value):
        self.__usedIdiom = value

    def addIdiom(self,value):
        self.__usedIdiom[value]=value;
          
        

    def get_state(self):
        return self.__state


    def set_state(self, value):
        self.__state = value

        #房间状态
        
        
        
    def addUser(self,userObj):
        '''
                        添加用户到房间
        @param userObj:用户对象
        @return: (room,userObj) 
    
        '''
        if not userObj:
            return(None,None);
        if not self.__roomOwner:
           userObj.set_user_is_room_owner(True); 
           self.__roomOwner=userObj;
            
        self.__userList.append(userObj);
        self.__userNameList[userObj.get_user_name()]=userObj;
        return (self,userObj);
    def delUser(self,userObj):
        '''
                        删除房间中的用户
        @param userObj:用户对象
        @return: (room,userObj) 
    
        '''
        if not userObj:
            return(None,None);
        nextUser=None;
        try:
            indexTmp=self.__userList.index(userObj)+1;
            nextUser=self.__userList[indexTmp];
        except:
            pass;        
        
        if self.__roomOwner:
           if self.__roomOwner.get_user_id()==userObj.get_user_id():
               self.__roomOwner=nextUser;         
        try:
            self.__userList.remove(userObj);                         
        except:
            pass;        
        try:
            del self.__userNameList[userObj.get_user_name()];                         
        except:
            pass;
        userObj.set_user_state(Config.I_USER_GAME_STATE_INIT);
        userObj.set_inroom(Config.I_SYS_ZERO);
        return (self,userObj);
        

    def get_room_owner(self):
        return self.__roomOwner


    def set_room_owner(self, value):
        self.__roomOwner = value


    def get_user_name_list(self):
        return self.__userNameList


    def set_user_name_list(self, value):
        self.__userNameList = value


    def get_room_pri(self):
        return self.__roomPri


    def set_room_pri(self, value):
        self.__roomPri = value


    def get_is_show(self):
        return self.__isShow


    def set_is_show(self, value):
        self.__isShow = value


    def get_user_list(self):
        return self.__userList


    def set_user_list(self, value):
        self.__userList = value

    def get_id(self):
        return self.__Id


    def get_room_name(self):
        return self.__RoomName


    def get_upper_limit(self):
        return self.__UpperLimit


    def get_created(self):
        return self.__Created


    def get_creator(self):
        return self.__Creator


    def get_first_name(self):
        return self.__FirstName


    def get_introduce(self):
        return self.__Introduce


    def get_commend(self):
        return self.__Commend


    def get_room_type(self):
        return self.__RoomType


    def set_id(self, value):
        self.__Id = value


    def set_room_name(self, value):
        self.__RoomName = value


    def set_upper_limit(self, value):
        self.__UpperLimit = value


    def set_created(self, value):
        self.__Created = value


    def set_creator(self, value):
        self.__Creator = value


    def set_first_name(self, value):
        self.__FirstName = value


    def set_introduce(self, value):
        self.__Introduce = value


    def set_commend(self, value):
        self.__Commend = value


    def set_room_type(self, value):
        self.__RoomType = value
    roomOwner = property(get_room_owner, set_room_owner, None, None)
    state = property(get_state, set_state, None, None)
    usedIdiom = property(get_used_idiom, set_used_idiom, None, None)
    currentWord = property(get_current_word, set_current_word, None, None)

